Spread 多单元格粘贴数据

发布时间:2012/11/13 00:11 发布者:葡萄城产品团队

返回博客中心

Excel 的使用中,我们都会用到复制某一单元格数据,粘贴到多个单元格中的功能。对于我们来说这个功能是很方便的。这个功能和 Excel 中的拖拽功能类似(拖拽有数据单元格到其他单元格域)。

下面我们就介绍怎么用 Spread 实现以上两种该功能。

1.Spread 的拖拽功能。我们仅仅需要添加一行代码:

 
FpSPread1.AllowDragFill=true;

复制代码

2.Spread 实现复制某一单元格数据,粘贴到多个单元格中的功能:
  
    在 Ctrl + C 赋值单元格之后,需要在 FpSpread1_ClipBoardPasting 事件中添加以下代码:

 
private void fpSpread1_ClipboardPasting(object sender, FarPoint.Win.Spread.ClipboardPastingEventArgs e)

        {

            FarPoint.Win.Spread.Model.CellRange cr = default(FarPoint.Win.Spread.Model.CellRange);

            string textdata = null;

            string[] a = null;

            string[] b = null;

            int rowcount = 0;

            int colcount = 0;

            cr = fpSpread1.Sheets[0].GetSelection(0);

            if (cr.RowCount > 1 | cr.ColumnCount > 1)

            {

                e.Handled = true;

                if (System.Windows.Forms.Clipboard.GetDataObject().GetDataPresent(System.Windows.Forms.DataFormats.Text))

                {

                    textdata = (string)System.Windows.Forms.Clipboard.GetDataObject().GetData(System.Windows.Forms.DataFormats.Text);

                    a = textdata.Split(new char[] { (char)13 });

                    rowcount = a.Length - 1;

                    b = a[0].Split(new char[] { (char)9 });

                    colcount = b.Length;

                    for (int i = cr.Row; i <= cr.Row + cr.RowCount - 1; i += rowcount)

                    {

                        for (int x = 0; x <= rowcount - 1; x++)

                        {

                            b = a[x].Split(new char[] { (char)9 });

                            for (int j = cr.Column; j <= cr.Column + cr.ColumnCount - 1; j += colcount)

                            {

                                for (int y = 0; y <= colcount - 1; y++)

                                {

                                    string myStr;

                                    myStr = b[0];

                                    fpSpread1.Sheets[0].SetValue(i + x, j + y, myStr.Trim((char)10, (char)30));

                                }

                            }

                        }

                    }

                }

            }

        }

    }

复制代码

Demo 下载:
编辑环境:Spread for WinForm 5.0 && VS 2010

MultipleCellsPaste.zip (40.02 K, 下载次数:23)

关于葡萄城

赋能开发者!葡萄城是专业的集开发工具、商业智能解决方案、低代码开发平台于一身的软件和服务提供商,为超过 75% 的全球财富 500 强企业提供服务。葡萄城专注控件软件领域30年,希望通过模块化的开发控件、灵活的低代码应用开发平台等一系列开发工具、解决方案和服务,帮助开发者快速响应复杂多变的业务需求,最大程度地发挥开发者的才智和潜能,让开发者的 IT 人生更从容更美好。

了解详情,请访问葡萄城官网